package com.mutouren.modules.org.model;

import java.util.Date;

import com.mutouren.common.entity.ValidState;
import com.mutouren.common.utils.StringUtils;
import com.mutouren.modules.org.entity.OrgType;

public class Organization {
	
	public static final int ROOT_ID = 0;
	public static final String ROOT_NAME = "组织机构";
	public static final String PATH_SEPARATOR = ",";
	
	private Integer orgId;
	private String orgName;
	private String code;
	private String orgType;
	private Integer parentId;
	private String parentIdPath;
	private Integer sequence;
	private String phone;
	private String description;
	private ValidState validState;
	private Date createTime;
	private Date updateTime;
	
	private String chineseParentPath;
	
	public Organization() {
		this.validState = ValidState.Normal;
	}
	
	public static Organization createRoot() {
		Organization result = new Organization();
		result.setOrgId(Organization.ROOT_ID);
		result.setOrgName(Organization.ROOT_NAME);
		result.setOrgType(OrgType.ORG.toString());
		result.setParentId(0); //? bug
		result.setParentIdPath("");
		result.setValidState(ValidState.Normal);
		return result;
	}
	
	public boolean isRoot() {
		return this.getOrgId().equals(ROOT_ID);
	}	
	
	public Integer getOrgId() {
		return orgId;
	}
	public void setOrgId(Integer orgId) {
		this.orgId = orgId;
	}
	public String getOrgNameWithFlag() {
		if (isRoot()) {
			return orgName;
		} else {
			return String.format("%s-%s", this.getDisplayOrgType(), this.getOrgName());
		}
	}	
	public String getOrgName() {
		return orgName;
	}
	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getDisplayOrgType() {
		return OrgType.display(OrgType.valueOf(orgType));
	}		
	public String getOrgType() {
		return orgType;
	}
	public void setOrgType(String orgType) {
		this.orgType = orgType;
	}
	public Integer getParentId() {
		return parentId;
	}
	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}
	public String getParentIdPath() {
		return parentIdPath;
	}
	public void setParentIdPath(String parentIdPath) {
		this.parentIdPath = parentIdPath;
	}
	public Integer getSequence() {
		return sequence;
	}
	public void setSequence(Integer sequence) {
		this.sequence = sequence;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getDisplayValidState() {
		return ValidState.display(validState);
	}	
	public ValidState getValidState() {
		return validState;
	}
	public void setValidState(ValidState validState) {
		this.validState = validState;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public Date getUpdateTime() {
		return updateTime;
	}
	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	public String getChineseParentPath() {
		return chineseParentPath;
	}
	public void setChineseParentPath(String chineseParentPath) {
		this.chineseParentPath = chineseParentPath;
	}
	public String getChineseAllPath() {
		if (this.orgId == ROOT_ID) {
			return "";
		} else {
			return String.format("%s/%s", this.getChineseParentPath(), this.getOrgName());
		}
	}	
	
	public String getSelfIdPath() {
		return buildSelfIdPath();
	}
	
	public String buildSelfIdPath() {
		if (this.getOrgId().equals(ROOT_ID)) {
			return String.format("%d%s", ROOT_ID, PATH_SEPARATOR);
		} else {
			if (StringUtils.isNullOrEmpty(this.getParentIdPath())) {
				throw new IllegalArgumentException(
						"parentOrg.getParentIdPath() is null or empty");
			}
			return String.format("%s%d%s", this.getParentIdPath(), this.getOrgId(), PATH_SEPARATOR);
		}
	}	
}
